<?php

namespace Hilaoyu\LaravelExtend\Services\AccessControl;


use Hilaoyu\LaravelExtend\Services\Auth\Traits\ModelWithLoginRefreshTrait;
use Hilaoyu\Utils\UtilSupport;
use Hilaoyu\LaravelExtend\Model;

abstract class AbstractModelAccessControlRolesEntitiesRelation extends Model
{



    protected $table = 'access_control_roles_entities_relation';
    protected $primaryKey = null;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'role_id' ,
        'entity_id' ,
        'action' ,
        'val' ,
    ];

    protected $appends = [
        'entity_name',
        'type',
        'type_name',
        'action_name',
    ];


    abstract static protected function classNameModelAccessControlEntities();
    static protected $_access_control_entities = null;

    public function getValidationRules()
    {

        $role_action_in = '';
        $class_name_model_access_control_entities = static::classNameModelAccessControlEntities();
        if($class_name_model_access_control_entities){
            $entities_model = UtilSupport::makeClassSingle(static::classNameModelAccessControlEntities());

            $role_action_in = '|in:'.collect($entities_model::GetAllowActions())->collapse()->keys()->join(',');
        }


        $rules = [
            'role_id' => 'required|string|max:36' ,
            'entity_id' => 'required|string|max:36' ,
            'action' => 'required|string|max:20' .$role_action_in,
        ];

        return $rules;
    }



    static public function getAccessControlEntities($id='',$key=''){
        $id = trim($id);
        $key = trim($key);

        $class_name_model_access_control_entities = static::classNameModelAccessControlEntities();
        if(!$class_name_model_access_control_entities){
            return null;
        }

        if(!static::$_access_control_entities){
            $entities_model = UtilSupport::makeClassSingle($class_name_model_access_control_entities);
            static::$_access_control_entities = $entities_model->get()
                ->keyBy($entities_model->getKeyName());
        }

        if($id){
            $model = static::$_access_control_entities->get($id);

            return $key ? data_get($model,$key) : $model;
        }

        return static::$_access_control_entities;
    }

    public function getEntityNameAttribute()
    {
        return UtilSupport::tryQuiet(function (){
            return static::getAccessControlEntities($this->getAttribute('entity_id'),'name');
        },'');
    }
    public function getTypeAttribute()
    {
        return UtilSupport::tryQuiet(function (){
            return static::getAccessControlEntities($this->getAttribute('entity_id'),'type');
        },'');
    }

    public function getTypeNameAttribute()
    {
        return UtilSupport::tryQuiet(function (){
            return static::getAccessControlEntities($this->getAttribute('entity_id'),'type_name');
        },'');
    }
    public function getActionNameAttribute()
    {

        return UtilSupport::tryQuiet(function (){
            return UtilSupport::makeClassSingle(static::classNameModelAccessControlEntities())::GetAllowActions($this->getAttribute('type') .'.' .$this->getAttribute('action'),'');
        },'');
    }

}
